// MySQL/connect.js
const mysql = require('mysql2');

const pool = mysql.createPool({
  connectionLimit: 10,
  host: 'localhost',
  user: 'root',
  password: 'root',
  database: 'students',
  multipleStatements: true,
});

// 修改 executeQuery 以支持 Promise
function executeQuery(query, params) {
  return new Promise((resolve, reject) => {
    pool.query(query, params, (error, results, fields) => {
      if (error) {
        // 在发生错误时，reject Promise 并传递错误对象
        reject(error);
      } else {
        // 在查询成功时，resolve Promise 并传递结果对象
        resolve(results);
      }
    });
  });
}

module.exports = executeQuery;
